#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid)
    {
        int n = grid.size();
        for (int i = 0; i < n; i++)
        {
            int m = grid[i][max_element(grid[i].begin(), grid[i].end()) - grid[i].begin()];
            grid[i].push_back(m);
        }
        vector<int> colmax;
        for (int j = 0; j < n; j++)
        {
            int m = -1;
            for (int i = 0; i < n; i++)
                m = max(m, grid[i][j]);
            colmax.push_back(m);
        }
        grid.push_back(colmax);
        int count = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                count += min(grid[i][n], grid[n][j]) - grid[i][j];
        }
        return count;
    }
};